package com.jhgk.jhcee.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jhgk.jhcee.common.service.RedisService;
import com.jhgk.jhcee.data.auth.entity.SysUserInfo;
import com.jhgk.jhcee.system.domain.model.SysMenuMetaModel;
import com.jhgk.jhcee.system.service.SysMenuCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: lemon_xzp
 * @Date: 2021/9/22 17:58
 */
@Service(value = "SysMenuCacheServiceImpl")
public class SysMenuCacheServiceImpl implements SysMenuCacheService {

    @Autowired
    private RedisService redisService;
    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.menu}")
    private String REDIS_KEY_MENU;

    @Override
    public List<SysMenuMetaModel> getMenu(Long userId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_MENU + ":" + userId;
        Object o = redisService.get(key);
        List<SysMenuMetaModel> menuMetaModels = new ArrayList<>();
        if (null != o) {
            BeanUtil.copyProperties(o, menuMetaModels);
        }
        return menuMetaModels;
    }

    @Override
    public Boolean delMenu(Long userId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_MENU + ":" + userId;
        return redisService.del(key);
    }

    @Override
    public void setMenu(Long userId, List<SysMenuMetaModel> menuMetaModels) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_MENU + ":" + userId;
        redisService.set(key, menuMetaModels, REDIS_EXPIRE);
    }
}
